package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.FrameLayout;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.transport.MapGoodsBiddingEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsSourceAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GoodsSourceWindow extends BasePopWindow {

    private GoodsSourceAdapter goodsSourceAdapter;
    private int page = 1;
    private int size = 10;
    XRecyclerView recyclerView;

    public GoodsSourceWindow(Context context) {
        super(context);
        setHeight(dip2px(context,490));
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_goods_source;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView  = holder.getPop().findViewById(R.id.goods_type_list);
        goodsSourceAdapter = new GoodsSourceAdapter(mContext, null);
        goodsSourceAdapter.setItemSelect(true);
        goodsSourceAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(goodsSourceAdapter);
        goodsSourceAdapter.setOnItemClick(new AdapterOnItemClick<MapGoodsBiddingEntity>() {
            @Override
            public void onItemClick(MapGoodsBiddingEntity myGoodsSourceListEntity, int position) {
                onResultClick.result(myGoodsSourceListEntity, position, null);
                dissMiss();
            }
        });
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getData(false);
            }

            @Override
            public void onLoadMore() {
                getData(true);
            }
        });
        FrameLayout close = holder.getPop().findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });

        //getData(false);
    }

    public  int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void setData(List<MapGoodsBiddingEntity> list, int position) {
        goodsSourceAdapter.setDatas(list);
        goodsSourceAdapter.setDefItem(position);
    }


    public void setDefItem(int position) {
        goodsSourceAdapter.setDefItem(position);
    }


    private void getData(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        APIManager.getInstance().getTransAPI().getMyGoodsSourceList(size, page).enqueue(new Callback<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> call, Response<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (!response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(mContext, response.body().getMsg());
                    return;
                }
                showList(isLoadMore,response.body().getData().getList());
                // activity.showStateView(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    private void showList(boolean isLoadMore,List<MapGoodsBiddingEntity> listEntities){
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        if (isLoadMore) {
            goodsSourceAdapter.addDatas(listEntities);
        } else {

            goodsSourceAdapter.setDatas(listEntities);
        }
    }

}
